﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
using MITI;
using System.Data.SqlServerCe;
using HtmlAgilityPack;

namespace WBrowser.MY_CODE
{
    public partial class ReadHtmlBookmark : Form
    {
        private string _filePath;
        private DataSet _dataSet;
        private DataView _dataView;
        private List<string> _forumType;

        public ReadHtmlBookmark()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "html files (*.html)|*.html|xml files (*.xml)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            string url;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.Load(openFileDialog1.FileName);
                    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]"))
                    {
                        url = node.Attributes["href"].Value;
                    }
                    _forumType = new List<string>();
                    //_dataSet = CreateDataSetForumList(openFileDialog1.FileName);

                    //Add gridview
                    _dataView = _dataSet.Tables[0].DefaultView;
                    gridViewForums.DataSource = _dataView;
                    gridViewForums.Columns[0].Frozen = true;
                    gridViewForums.Columns[1].Frozen = true;
                    gridViewForums.Columns[2].Frozen = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

            
        }

        //public DataSet CreateDataSetForumList(string filename)
        //{
        //    XmlDocument doc;
        //    XmlNode node;
        //    XmlNodeList nodeList;

        //    DataSet ds = new DataSet();
        //    DataTable db = new DataTable("Forums");
        //    DataRow dr;

        //    int columnNum;
        //    //Init forum type
        //    _forumType.Clear();
        //    _forumType.Add("");

        //        doc = new XmlDocument();
        //        doc.Load(filename);
        //        node = doc.DocumentElement;
        //        nodeList = node.ChildNodes;

        //        //just add columnNum columns
        //        columnNum = nodeList.Count;

        //        //Create datatable
        //        if (db.Columns.Count == 0)
        //        {
        //            db = new DataTable();
        //            for (int j = 0; j < columnNum; j++) //just add columnNum columns
        //            {
        //                db.Columns.Add(nodeList[j].Name);
        //            }
        //        }

        //        //Add data in table
        //        dr = db.NewRow();
        //        for (int j = 0; j < columnNum; j++) //just add columnNum columns
        //        {
        //            //Add forum type
        //            if (nodeList[j].Name == "Type" && !_forumType.Contains(nodeList[j].InnerText))
        //                _forumType.Add(nodeList[j].InnerText);

        //            //Add data in table
        //            dr[nodeList[j].Name] = nodeList[j].InnerText;
        //        }
        //        db.Rows.Add(dr);

        //    //Add datatable in dataset
        //    ds.Tables.Add(db);
        //    return ds;
        //}
    }
}
